//
//  LoginBuyyerViewController.m
//  SmartCoin
//
//  Created by apple on 2018/3/7.
//  Copyright © 2018年 Cynic. All rights reserved.
//

#import "LoginBuyyerViewController.h"
#import "BuyyerMainViewController.h"
#import "ConnectWithPhoneViewController.h"
#import <UMSocialCore/UMSocialCore.h>
#import <ShareSDK/ShareSDK.h>
#import "DetailBuyyerViewController.h"
@interface LoginBuyyerViewController ()
@property (nonatomic ,assign)BOOL loginsuccess;
@end

@implementation LoginBuyyerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = bggrayColor;
    [self viewLoad];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}

-(void)viewLoad{
    UIImageView *topLineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, cxsingler.topdis -  cxsingler.navitop, cx_wid, cx_wid / 750 * 746 + cxsingler.navitop)];
    topLineView.image = [UIImage imageNamed:@"path_4 copy"];
    [self.view addSubview:topLineView];
    
    CGFloat wid = 299.0 / 375 * cx_wid;
    CGFloat hit = 327.0 / 375 * cx_wid;
    CGFloat bottomdis = 120.0 / 375 * cx_wid;
    
    UIView *centerView = [[UIView alloc] init];
    centerView.bounds = CGRectMake(0, 0, wid, hit);
    centerView.center = CGPointMake(cx_wid / 2, cx_hit / 2);
    centerView.layer.shadowOffset = CGSizeMake(3, 3);
//    centerView.layer.opacity = 0.4;
    centerView.backgroundColor = [UIColor whiteColor];
    centerView.layer.borderColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4].CGColor;
    
    UIImageView *centerimg = [[UIImageView alloc] init];
    centerimg.image = [UIImage imageNamed:@"tuan"];
    [centerView addSubview:centerimg];
    [centerimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(centerView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(135.0 / 375 * cx_wid, 175.7 / 375 * cx_wid));
        make.top.mas_equalTo(38.9 / 375 * cx_wid);
    }];
    
    UIButton *loginbtn = [[UIButton alloc] init];
    loginbtn.layer.cornerRadius = 20;
    [loginbtn addTarget:self action:@selector(logintaped:) forControlEvents:UIControlEventTouchUpInside];
    [loginbtn setTitle:@"微信登录" forState:UIControlStateNormal];
    [loginbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.frame = CGRectMake(0, 0, cx_wid - 46 - 38 * 2, 40);
    layer.colors = @[(id)[CXSinglerModel colorWithHex:@"#FF7F5A" alpha:1].CGColor,(id)[CXSinglerModel colorWithHex:@"#FE4438" alpha:1].CGColor];
    layer.startPoint = CGPointMake(0, 0);
    layer.endPoint = CGPointMake(1, 0);
    [loginbtn.layer addSublayer:layer];
    [centerView addSubview:loginbtn];
    [loginbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(22);
        make.bottom.mas_equalTo(-26.0 / 375 * cx_wid);
        make.right.mas_equalTo(-22);
        make.height.mas_equalTo(40);
    }];
    [self.view addSubview:centerView];
    
    UIButton *pop = [[UIButton alloc] initWithFrame:CGRectMake(0, cxsingler.topdis , 44, 44)];
    [pop setImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [pop addTarget:self action:@selector(pop:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pop];
}

-(void)pop:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)logintaped:(UIButton *)sender{
    [SVProgressHUD showWithStatus:@"登录中"];
    _loginsuccess = false;
    [self performSelector:@selector(showhidden) withObject:nil afterDelay:10];
    //微信授权参数获取
    [ShareSDK getUserInfo:SSDKPlatformTypeWechat onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        if (error) {
            [SVProgressHUD showErrorWithStatus:@"登录失败"];
        } else {
            if (user.uid.length <3) {
                [SVProgressHUD showErrorWithStatus:@"登录失败"];
                return ;
            }
            [SVProgressHUD showSuccessWithStatus:@"授权成功"];
            _loginsuccess = YES;
            cxsingler.resp = user;
            // 授权信息
            NSLog(@"Wechat uid: %@", user.uid);
            [self weixinloginwithcode:[user.rawData objectForKey:@"unionid"]];
            
        }
    }];
}

-(void)showhidden{
    if (_loginsuccess) {
        return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showErrorWithStatus:@"连接超时"];
#pragma mark--autoweixinlogin
    });
}



/**
 微信id 员工登录

 @param openid 员工的微信id
 */
-(void)weixinloginwithcode:(NSString *)openid{
    WEAKSELF;
    NetWorkModel *model = [[NetWorkModel alloc] init];
    model.sendMessage = ^(NSString *result) {
        if ([result isEqual:@"success"]) {
            DetailBuyyerViewController *nextVC = [[DetailBuyyerViewController alloc] init];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.navigationController pushViewController:nextVC animated:YES];
                weakSelf.navigationController.interactivePopGestureRecognizer.enabled = NO;
            });
        }
        else if ([result isEqual:@"newin"]){
            dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"暂未关联入，请联系商户"];
            });
        }
        else{
            dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:result];
            });
        }
    };
    [model wexinLogin:openid];
}

//-(void)connectphone{
//    ConnectWithPhoneViewController *nextVC = [[ConnectWithPhoneViewController alloc] init];
//    [self.navigationController pushViewController:nextVC animated:YES];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
